package com.lei.datastructure.linkedlist;

/**
 * @Author LeiXiaoLei
 * @Date 2022/7/22 11:37
 * @Version 1.0
 */
public class LinkedList2<E> {
    private final Node dummyHead;
    private int size;

    public LinkedList2() {
        dummyHead = new Node(null, null);
        this.size = 0;
    }

    public int getSize() {

        return this.size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Add failed. Illegal index.");
        Node head = dummyHead;
        if (index == 0 && dummyHead.next == null) {
            head.next = addElement(head, index, e);
            head.next.next = null;
        }
        addElement(head.next, index, e);
        size++;
    }

    private Node addElement(Node node, int index, E e) {
        if (index == 0) return new Node(e, node);
        node.next = addElement(node.next, index - 1, e);
        return node;
    }

    public void addFirst(E e) {
        if (dummyHead.next == null) dummyHead.next = new Node(e);
        else {
            dummyHead.next = new Node(e, dummyHead.next);
        }
        ++size;
    }

    public void addLast(E e) {
        add(size, e);
    }

    public E get(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Get failed. Illegal index.");
        Node cur = dummyHead.next;
        cur = getElement(cur, index);
        return cur.e;
    }

    private Node getElement(Node node, int index) {
        if (index == 0) return node;
        return getElement(node.next, --index);
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    public E remove(int index) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Get failed. Illegal index.");
        Node node = null;
        if (index == 0) {
            node = dummyHead.next;
            if (dummyHead.next.next != null) dummyHead.next = dummyHead.next.next;
            else dummyHead.next = null;
        } else {
            node = removeElement(dummyHead.next, index);
        }
        size--;
        return node.e;
    }

    // 从链表中删除元素e
    public void removeElement(E e){
        Node prev = dummyHead;
        while(prev.next != null){
            if(prev.next.e.equals(e))
                break;
            prev = prev.next;
        }

        if(prev.next != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            size --;
        }
    }

    private Node removeElement(Node node, int index) {
        if (index == 1) {
            if (node.next.next != null) return node.next = node.next.next;
            else return node.next = null;
        }
        return removeElement(node.next, --index);
    }

    public Node removeElements(Node head, E e) {
        if (head == null) return null;
        head.next = removeElements(head.next, e);
        return head.e == e ? head.next : head;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public void set(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Set failed. Illegal index.");
        if (index == 0) dummyHead.next.e = e;
        else
            setElement(dummyHead.next, index, e);
    }

    private Node setElement(Node node, int index, E e) {
        if (index == 0) {
            node.e = e;
            return node;
        }
        return setElement(node.next, --index, e);
    }

    public boolean contains(E e) {
        Node cur = dummyHead.next;
        Node node = containsElement(cur, e);
        return node != null;
    }

    private Node containsElement(Node node, E e) {
        if (node == null) return null;
        if (node.e.equals(e)) return node;
        return containsElement(node.next, e);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null) {
            res.append(cur).append("->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }

    private class Node {
        public E e;
        public Node next;

        public Node() {
            this(null, null);
        }

        public Node(E e) {
            this(e, null);
        }

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    public static void main(String[] args) {
        LinkedList2<Integer> linkedList = new LinkedList2<>();
        System.out.println(linkedList);
        for (int i = 0; i < 5; i++) {
            linkedList.addLast(i);
            System.out.println(linkedList);
        }
        linkedList.add(2, 666);
        System.out.println(linkedList);
        Integer remove = linkedList.remove(2);
        System.out.println(linkedList);
        linkedList.removeFirst();
        System.out.println(linkedList);
        System.out.println(linkedList.get(2));
        linkedList.set(1, 666);
        System.out.println(linkedList);
        System.out.println(linkedList.contains(666));
    }
}
